const router = require('express').Router()
const R = require('ramda')
const U = require('../utils')
const Node = require('../models/Node')

router.param('id',(req,res,next,id) => {
  req.nid = id
  next()
})

function wrap_params(req,res,next){
  if(R.isNil(req.body.title)){
    res.json(U.err('请提供需要修改的标题'))
    return
  }
  req.title = req.body.title
  next()
}

router.route('/:id')
 .put(wrap_params,(req,res,next) => {
   Node.put(req.nid,req.title).then(result => {
     res.json(U.ok('更新节点名结束'))
   })
 })
 .delete((req,res,next) => {
   Node.del(req.nid).then(result => {
     res.json(U.ok('删除节点结束'))
   })
 })
 

module.exports = router